<?php

namespace Home\Controller;

use Common\Controller\BaseController;

class PublicController extends BaseController {
    
    /**
     *  用户注册
     */
    public function register() {
        $Users = D("Users");
        
        if(!$Users->create()) {
            $this->error($Users->getError());
        }
        $Users->password      = md5($Users->password);
        $res                 = $Users->add();
        
        if($res){
            $this->success('注册成功',1);
        }else {
            $this->success('注册失败');
        }
    
    }
    
    /**
     *  用户登录
     */
    public function login() {
    
        $strUserName = I('userName');
        $strPassword = I('password');
        if(empty($strUserName)) {
            $this->error('用户名不能为空');
        }
        if(empty($strPassword)){
            $this->error('密码不能为空');
        }
    
        $arrAuthInfo = M("Users")->field('uid,userName,phoneNumber')->where(array('userName'=>$strUserName, 'password'=>md5($strPassword)))->find();
        if ($arrAuthInfo === false || $arrAuthInfo == null){
            $this->error('账号或密码错误，请重新输入！');
        }
        session('iUserId',$arrAuthInfo['uid']);

        $returnData = array(
            'uid'           => $arrAuthInfo['uid'],
            'userName'      => $arrAuthInfo['username'],
            'phoneNumber'   => $arrAuthInfo['phonenumber']
        );
        $this->success('登录成功！',1,$returnData);
    
    }
    
    /**
     *  用户登出
     */
    public function logout() {
        if(isset($this->iUserId)) {
            session_destroy();
            $this->success('登出成功！');
        }
        else {
            $this->error('已经登出！');
        }
    }
    
    
}